package com.ruoyi.project.api.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.income.domain.BinsMerchantRecode;
import com.ruoyi.project.income.service.IBinsMerchantRecodeService;
import com.ruoyi.project.price.domain.BinsAccount;
import com.ruoyi.project.price.domain.BinsAccountTrace;
import com.ruoyi.project.price.service.IBinsAccountService;
import com.ruoyi.project.price.service.IBinsAccountTraceService;
import com.ruoyi.project.user.domain.UserInfo;
import com.ruoyi.project.user.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title : UserController
 * @Author : Administrator
 * @Date : 2025/4/9
 * @Version : 1.0.0
 * @Description :
 */
@RestController
@RequestMapping("/api/user")
public class UserController extends BaseController {

    @Autowired
    private IUserInfoService userInfoService;
    @RequestMapping("/info")
    public AjaxResult getUserInfo(String userId) {
        UserInfo userInfo = userInfoService.selectUserInfoByUserId(userId);
        return AjaxResult.success(userInfo);
    }
    @Autowired
    private IBinsAccountService accountService;
    @RequestMapping("/balance")
    public AjaxResult getUserBalance(String userId) {
        Map map = new HashMap<>();
        logger.info("用户余额请求参数：{}",userId);
        BinsAccount account = accountService.selectBinsAccountByUserId(userId);
        if (account==null){
            throw new RuntimeException("用户账户咱不存在");
        }
        map.put("balance",account.getTotalAmount());
        return AjaxResult.success(map);
    }

    @Autowired
    private IBinsAccountTraceService binsAccountTraceService;

    /**
     * 钱包使用记录
     * @param binsAccountTrace
     * @return
     */
    @RequestMapping("/walletRecodes")
    public AjaxResult list(BinsAccountTrace binsAccountTrace)
    {
        startPage();
        List<BinsAccountTrace> list = binsAccountTraceService.selectBinsAccountTraceList(binsAccountTrace);
        TableDataInfo<BinsAccountTrace> table= getDataTable(list);
        Map map = new HashMap<>();
        map.put("list",list);
        map.put("total",table.getTotal());
        BinsAccountTrace binsAccountTrace_sum = binsAccountTraceService.selectBinsAccountTraceSUM(binsAccountTrace);
        if (binsAccountTrace_sum!=null){
            map.put("totalAmount",binsAccountTrace_sum.getTotalAmount());
        }
        return AjaxResult.success(map);
    }


}
